package com.vip.zuul.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Description : 
 * @Author Evan Liu
 * @Date 2018年11月16日
 */
@Configuration 
public class VIPZuulConfiguration{
//public class VipZuulConfiguration extends ZuulProxyAutoConfiguration {
    @Autowired
    ZuulProperties zuulProperties;
    
    @Autowired
    private SpringClientFactory springClientFactory;
    
    @Autowired
    ServerProperties server;
    
	@Autowired
	private ProxyRequestHelper proxyRequestHelper;
	
	@Autowired
    private LoadBalancerClient loadBalancer;
	
	@Autowired
    private LoadBalancerRequestFactory requestFactory;
	
	//默认启动的过滤器
	org.springframework.cloud.netflix.zuul.filters.pre.ServletDetectionFilter 	servletDetectionFilter;
	org.springframework.cloud.netflix.zuul.filters.pre.Servlet30WrapperFilter 	servlet30WrapperFilter;
	org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter 	formBodyWrapperFilter;
	org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter 				debugFilter;
	//判断当前请求上下文中是否存在forward.do和serviceId参数
	org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter 		preDecorationFilter;
	
	//该过滤器只对请求上下文中存在serviceId参数的请求进行处理，即只对通过serviceId配置路由规则的请求生效
	org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter 	ribbonRoutingFilter;
	//只对通过url配置路由规则的请求生效。而该过滤器的执行逻辑就是直接向routeHost参数的物理地址发起请求
	org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter simpleHostRoutingFilter;
    
    org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter 	SendForwardFilter;
    org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter 	SendResponseFilter;
    
    /*@Bean
    public LoadBalancerInterceptor buildLoadBalancerInterceptor() {
    	return new LoadBalancerInterceptor(loadBalancer,requestFactory);
    }*/
    
    @Bean //注册了一个路由刷新监听器，默认实现是ZuulRefreshListener.class，这个是我们动态路由的关键
    public ApplicationListener<ApplicationEvent> zuulRefreshRoutesListener() {
        return new VIPZuulRefreshListener();
    }
    
    @Bean(name="VipZuulRouteLocator")
    public VIPZuulRouteLocator routeLocator() {
    	return new VIPZuulRouteLocator(this.server.getServlet().getServletPrefix(), this.zuulProperties,springClientFactory);
    }
    
	/*@Bean
	public AuthorityPreFilter matchFilter() {
		return new AuthorityPreFilter();
	}*/
	
	/*@Bean
	@Override
	public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper, ZuulProperties zuulProperties,
			ApacheHttpClientConnectionManagerFactory connectionManagerFactory,
			ApacheHttpClientFactory httpClientFactory) {
		return new RewriteSimpleHostRoutingFilter(helper, zuulProperties,connectionManagerFactory,httpClientFactory); 
	}*/
	
	/*@Bean
	public SimpleHostRoutingFilter simpleHostRoutingFilter(ProxyRequestHelper helper,ZuulProperties zuulProperties,CloseableHttpClient httpClient) {
		return new RewriteSimpleHostRoutingFilter(proxyRequestHelper, zuulProperties,httpClient); 
	}*/
    

}


